/**
 * 
 */
package de.akabahn.ebd.control;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

/**
 * @author Peter Eimann
 */
public class EBD_PreferenceActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getFragmentManager().beginTransaction()
				.replace(android.R.id.content, new PreferencesFragment())
				.commit();
	}

	public class PreferencesFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			addPreferencesFromResource(R.xml.preferences);
		}

		@Override
		public void onResume() {
			super.onResume();
			
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(EBD_PreferenceActivity.this);
			prefs.registerOnSharedPreferenceChangeListener(this);

			updateSummary();
		}

		@Override
		public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
			updateSummary();
		}

		private void updateSummary() {
			ListPreference lPref = (ListPreference)getPreferenceScreen().findPreference("pref_key_num_fst");
			lPref.setSummary(lPref.getEntry());

			EditTextPreference tPref = (EditTextPreference)getPreferenceScreen().findPreference("pref_key_fst_server");
			tPref.setSummary(tPref.getText());
			tPref = (EditTextPreference)getPreferenceScreen().findPreference("pref_key_fst_port");
			tPref.setSummary(tPref.getText());
			tPref = (EditTextPreference)getPreferenceScreen().findPreference("pref_key_fstctrl_server");
			tPref.setSummary(tPref.getText());
			tPref = (EditTextPreference)getPreferenceScreen().findPreference("pref_key_fstctrl_port");
			tPref.setSummary(tPref.getText());
			tPref = (EditTextPreference)getPreferenceScreen().findPreference("pref_key_dbd_server");
			tPref.setSummary(tPref.getText());
			tPref = (EditTextPreference)getPreferenceScreen().findPreference("pref_key_dbd_port");
			tPref.setSummary(tPref.getText());
		}
	}
}
